package com.rongji.dfish.demo.editor.controller;

import java.math.BigInteger;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rongji.dfish.base.Utils;
import com.rongji.dfish.base.crypt.CryptFactory;
import com.rongji.dfish.base.crypt.JCECryptor;
import com.rongji.dfish.base.crypt.StringCryptor;
import com.rongji.dfish.framework.controller.BaseController;
import com.rongji.dfish.misc.chinese.Trad2SimpConverter;
import com.rongji.dfish.misc.origin.OriginMatcher;
import com.rongji.dfish.ui.command.JSCommand;
@Controller
@RequestMapping("/misc")
public class MiscController  extends BaseController{
	
	@RequestMapping("/findOrigin")
	@ResponseBody
	public Object findOrigin(HttpServletRequest request) throws Exception {
		String src=Utils.getParameter(request, "src");
		OriginMatcher matcher = OriginMatcher.getInstance();
		OriginMatcher.MatchResult mr=matcher.match(src);
		String result="[未知]";
		int conf=0;
		if(mr!=null){
			result=mr.getCode();
			conf=mr.getConfidence();
		}
		return new JSCommand("VM(this).fv('result','"+result+"');VM(this).fv('confidence','"+conf+"');");
	}
	
	@RequestMapping("/chineseC2T")
	@ResponseBody
	public Object chineseC2T(HttpServletRequest request) throws Exception {
		boolean t2c="T2C".equals(request.getParameter("cmd"));
		if(t2c){
			Trad2SimpConverter c=new Trad2SimpConverter();
			String src=Utils.getParameter(request, "TC");
			String res=c.convert(src);
			return new JSCommand("VM(this).fv('SC','"+Utils.escapeXMLword(res)+"')");
		}else{
			Trad2SimpConverter c=new Trad2SimpConverter(Trad2SimpConverter.SIMPLIFIED_TO_TRADITIONAL);
			String src=Utils.getParameter(request, "SC");
			String res=c.convert(src);
			return new JSCommand("VM(this).fv('TC','"+Utils.escapeXMLword(res)+"')");
		}
	}
	
	@RequestMapping("/encrypt")
	@ResponseBody
	public Object encrypt(HttpServletRequest request) throws Exception {
		boolean en="en".equals(request.getParameter("cmd"));
		String src=Utils.getParameter(request, "src");
		Object code=Utils.getParameter(request, "code");
		String alg=request.getParameter("alg");
		String encode=request.getParameter("encode");
		String prs=request.getParameter("prs");
		if(CryptFactory.AES.equals(alg)){
			code=JCECryptor.hex2byte("FE868B7887D68D3D4399EB60810D9631");//密钥
		}else if(CryptFactory.DES.equals(alg)){
			code=JCECryptor.hex2byte("3BDF58838F19161A");//密钥
		}else if(CryptFactory.TRIPLE_DES.equals(alg)){
			code=JCECryptor.hex2byte("EF1C3776A87AE602F1F120613BC1CBF29D9BF26D8062FB40");//密钥
		}else{
			//
			code=new BigInteger[] {
			          new BigInteger("12868221579326205383"),
			          new BigInteger("12400495569779260772208470728281091633857262584882396634232537673686136695792012744253415165357363898111835956182288689413415332700187138800163342528935995162909493886327294333783454800379993237576372926262416930647144015290354106493652889030302223064191324977053106297593241888901587847576970275305305448082887"),
			          new BigInteger("24572756090675436655558989348367435657229027934850039134723547928298759493459210750763275821415365998249385058114785981171525775397529434899057014728428426923144106791982259796514324321640848981001358248859407903752571447929139072117650100161243770428835841578424868766229345464526762630277584983436721460351347")};
		}
		if(Utils.isEmpty(alg)||alg.equals("NONE")){
			alg=null;
		}
		
		StringCryptor sc=CryptFactory.getStringCryptor(alg, encode, Integer.parseInt(prs),code);
		String res=null;
		if(en){
			res=sc.encrypt(src);
		}else{
			res=sc.decrypt(src);
		}
		return new JSCommand("VM(this).fv('result','"+Utils.escapeXMLword(res)+"')");
	}
}
